package com.leetcode.array.binarysearch.demo4;

/**
 * @Author Zan
 * @Create 2024/5/8 10:04
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/sqrtx/description/  x 的平方根
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int ans = -1;
        while (left <= right) {
            int mid = (right + left) / 2;
            if ((long) mid * mid <= x) { // 如果中间的平方小于等于x
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return ans;
    }
}
